<template>
	<view class="login">
		<!-- 顶部结构start -->
		<view class="nav">
			<view class="nav-left">
				<view class="nav-icon">
					<image src="../../static/images/home_active.png" mode="widthFix" class="img"></image>
				</view>
				<view class="nav-txet">
					Luckin Coffee
				</view>
			</view>
			<view class="nav-right" @click="goToIndex">
				先逛一逛
			</view>
		</view>
		<!-- 顶部结构end -->
		<!-- 文本结构 start -->
		<view class="section">

			<view class="section-name">
				欢迎回来
			</view>
			<view class="section-enname">
				please
			</view>
		</view>
		<!-- 文本结构 start -->
		<!-- 登录结构 start -->
		<view class="nav-login">
			<u-form :model="form" ref="uForms">
				<u-form-item label="手机号" prop="number">
					<u-input v-model="form.number" type="text" placeholder="请输入手机号" />
				</u-form-item>
				<u-form-item label="密码" prop="password">
					<u-input v-model="form.password" type="password" placeholder="请输入密码" />
				</u-form-item>
			</u-form>
			<view class="goTo" @click="goTos">
				忘记密码？
			</view>
		</view>
		<!-- 登录结构 end-->
		<button @click="submit" class="btn_1 common">登录</button>
		<button @click="show = true" class="btn_2 common">注册</button>
		<!-- 弹出层结构 start -->
		<u-popup v-model="show" mode="bottom" length="60%" :closeable="true" z-index="1">
			<view class="popup-box">
				<view class="popup-title">
					注册
				</view>
				<u-form :model="form" ref="uForm">
					<u-form-item label="手机号" prop="number">
						<u-input v-model="form.number" type="text" placeholder="请输入手机号" />
					</u-form-item>
					<u-form-item label="密码" prop="password">
						<u-input v-model="form.password" type="password" placeholder="请输入密码" />
					</u-form-item>
					<u-form-item label="昵称" prop="nickName">
						<u-input v-model="form.nickName" type="text" placeholder="请输入昵称" />
					</u-form-item>
				</u-form>
				<button type="default" @click="submits" class="btn_1 common">注册</button>
			</view>
		</u-popup>
		<!-- 弹出层结构 start -->

	</view>
</template>

<script>
	import request from "../../request/request.js"
	export default {
		data() {
			return {
				show: false,
				form: {
					number: '',
					password: "",
					nickName: ""
				},
				rules: {
					number: [{
						required: true,
						pattern: /^1[3-9]\d{9}$/,
						message: '请输入正确手机号',
						// trigger: ['blur', 'change']
					}],
					password: [{
						required: true,
						pattern: /^[A-Za-z]\w{5,15}$/,
						message: '请输入正确密码',
						// trigger: ['blur', 'change']
					}]
				},
				ruleser: {
					number: [{
						required: true,
						pattern: /^1[3-9]\d{9}$/,
						message: '请输入正确手机号',
						// trigger: ['blur', 'change']
					}],
					password: [{
						required: true,
						pattern: /^[A-Za-z]\w{5,15}$/,
						message: '请输入正确密码',
						// trigger: ['blur', 'change']
					}],
					nickName: [{
						required: true,
						pattern: /^[\u4e00-\u9fa5A-Za-z0-9]{1,5}$/,
						message: '请输入正确昵称',
						// trigger: ['blur', 'change']
					}]

				}
			}
		},
		methods: {
			//登录
			submit() {
				this.$refs.uForms.validate(valid => {
					if (valid) {

						this.goTo()
						console.log('验证通过');
					} else {
						console.log('验证失败');
					}
				});
			},
			goTo() {
				request({
					url: "/login",
					method: "POST",
					data: {
						password: this.form.password,
						phone: this.form.number
					},
					success: (res) => {
						let {
							token
						} = res.data
						uni.setStorageSync('token', token)
						console.log(res)
						if (res.data.code == 200) {
							uni.showToast({
								title: res.data.msg,
								icon: "none",
								mask: true,
								duration: 1000
							})
							setTimeout(() => {
								uni.switchTab({
									url: "../index/index"
								})
							}, 1000)
						}


					}
				})
			},
			goToLogin() {
				request({
					url: "/register",
					method: "POST",
					data: {
						password: this.form.password,
						phone: this.form.number,
						nickName: this.form.nickName
					},
					success: (res) => {
						console.log(res)
						if (res.code == 100) {
							uni.showToast({
								title: res.data.msg,
								icon: "none",
								mask: true,
								duration: 1000
							})
						}


					}
				})
			},
			goTos() {
				uni.navigateTo({
					url: "../email/email"
				})
			},
			goToIndex() {
				uni.switchTab({
					url: "/pages/index/index"
				})
			},
			//注册
			submits() {
				this.$nextTick(() => {
					this.$refs.uForm.setRules(this.ruleser);
					this.$refs.uForm.validate(valid => {
						if (valid) {
							this.goToLogin()
							console.log('验证通过');
						} else {
							console.log('验证失败');
						}
					});
				})
			}


		},
		onLoad() {
			uni.showToast({
				title: "请先登录",
				icon: "none",
				mask: true,
				duration: 1000
			})
		},
		onReady() {
			this.$refs.uForms.setRules(this.rules);
		}
	}
</script>

<style lang="less" scoped>
	.login {
		.nav {
			height: 120rpx;
			// border-bottom: 2rpx solid #ccc;
			padding: 0 30rpx;
			padding-bottom: 20rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.nav-left {
				flex: 7.5;
				display: flex;
				align-items: center;

				.nav-icon {
					width: 75rpx;
					height: 75rpx;

					.img {
						width: 100%;
					}
				}

				.nav-txet {
					color: #999;
					font-size: 38rpx;
					font-weight: bold;
					margin-left: 30rpx;
				}
			}

			.nav-right {
				color: #0c34ba;
				font-size: 34rpx;
				font-weight: bold;
				flex: 2.5;

			}
		}

		.section {
			margin: 0 30rpx;
			margin-top: 180rpx;

			.section-name {
				font-size: 80rpx;
				font-weight: bold;
			}

			.section-enname {
				margin: 50rpx 0;
				margin-bottom: 80rpx;
				font-size: 40rpx;
			}
		}

		.nav-login {
			.goTo {
				margin: 50rpx 30rpx;
				margin-bottom: 10rpx;
				float: right;
			}
		}

		.common {
			height: 100rpx;
			font-size: 34rpx;
			border-radius: 80rpx;
			margin: 80rpx 30rpx;
			line-height: 100rpx;
			text-align: center;
		}

		.btn_1 {
			color: #fff;
			background-color: #0c34ba;
			margin-top: 150rpx;
		}

		.btn_2 {
			border: 2rpx solid #ccc;
		}

		.popup-box {
			.popup-title {
				color: #999;
				font-size: 80rpx;
				font-weight: bold;
				padding: 50rpx;
				padding-top: 100rpx;


			}
		}

		::v-deep .u-border-bottom {
			border-bottom: 2rpx solid #ccc;
			margin: 0 50rpx;
		}

		::v-deep .u-form-item--left__content__label {
			width: 100rpx;
		}

		::v-deep .u-form-item--right__content__slot {
			margin-left: 30rpx;
		}

		::v-deep .u-form-item__message {
			display: none;
		}
	}
</style>
